/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.xy.controller;

import com.xy.config.AipFaceClient;
import com.xy.constants.RabbitConstant;
import com.xy.dto.ViewerDTO;
import com.xy.entity.GetPrintDataEntity;
import com.xy.excel.ViewerImporter;
import com.xy.excel.ViewerExcel;
import com.xy.service.IBlacklistService;
import com.xy.service.ITimeStageService;
import com.xy.service.IaccessQueryService;
import com.xy.tx.TxCommon;
import com.xy.wrapper.ViewerWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;
import javax.validation.Valid;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.BeanUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xy.entity.ViewerEntity;
import com.xy.vo.ViewerVO;
import com.xy.service.IViewerService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.tool.utils.DateUtil;
import org.springblade.core.excel.util.ExcelUtil;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

/**
 * 票证管理 控制器
 *
 * @author BladeX
 * @since 2023-07-18
 */
@RestController
@AllArgsConstructor
@RequestMapping("viewer")
@Api(value = "票证管理", tags = "票证管理接口")
public class ViewerController extends BladeController {

	private final IViewerService viewerService;
	private final IaccessQueryService iaccessQueryService;
	private final RabbitTemplate rabbitTemplate;
	private final IBlacklistService blacklistService;
	private final ITimeStageService timeStageService;

	/**
	 * 票证管理 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入viewer")
	public R<ViewerVO> detail(ViewerEntity viewer) {
		ViewerEntity detail = viewerService.getOne(Condition.getQueryWrapper(viewer));
		detail.setTimeStageList(timeStageService.getTimeStage(viewer.getId()));
		return R.data(ViewerWrapper.build().entityVO(detail));
	}
	/**
	 * 票证管理 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入viewer")
	public R<IPage<ViewerVO>> list(ViewerEntity viewer, Query query) {
//		if(Func.isBlank(viewer.getName()) && Func.isBlank(viewer.getTwoCode()) && Func.isBlank(viewer.getSwipeCard()) && Func.isBlank(viewer.getIdCard())
//			&& viewer.getViewerTypeId() == null && Func.isBlank(viewer.getMobile())
//		){
//			return R.data(null);
//		}
		QueryWrapper<ViewerEntity> queryWrapper = Condition.getQueryWrapper(viewer);
		queryWrapper.lambda().orderByDesc(ViewerEntity::getCreateTime);
		IPage<ViewerEntity> pages = viewerService.page(Condition.getPage(query),queryWrapper );
		IPage<ViewerVO> page = ViewerWrapper.build().pageVO(pages);
		return R.data(page);
	}

	/**
	 * 票证管理 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入viewer")
	public R<IPage<ViewerVO>> page(ViewerVO viewerVO, Query query) {
		IPage<ViewerVO> pages = viewerService.selectViewerPage(Condition.getPage(query), viewerVO);
		return R.data(pages);
	}
	/**
	 * 票证管理 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入viewer")
	public R save(@Valid @RequestBody ViewerEntity viewer) {
		viewerService.save(viewer);
		//推送至消息队列
//		QueueFaceDTO queueFaceDTO = BeanUtil.newInstance(QueueFaceDTO.class);
//		queueFaceDTO.setGroupId(viewer.getExhibitionId().toString());
//		queueFaceDTO.setViewerId(viewer.getId().toString());
//		queueFaceDTO.setPhotoUrl(viewer.getHeadUrl());
//		rabbitTemplate.convertAndSend(RabbitConstant.TOPIC_ADD_FACE_EXCHANGE,RabbitConstant.TOPIC_ADD_FACE_ROUTING_KEY_ONE,queueFaceDTO);


		return R.status(true);
	}

	/**
	 * 票证管理 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入viewer")
	public R update(@Valid @RequestBody ViewerEntity viewerEntity) {
		return R.status(viewerService.updateById(viewerEntity));
	}

	/**
	 * 票证信息采集
	 */
	@PostMapping("/collect")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "票证信息采集", notes = "传入viewer")
	public R collect(@Valid @RequestBody ViewerEntity viewerEntity) {
		try {
			return R.status(viewerService.collect(viewerEntity));
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	/**
	 * 信息采集
	 */
	@PostMapping("/getPrintData")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "获取打印数据", notes = "传入viewer")
	public R getPrintData( @RequestBody GetPrintDataEntity req) {
		try {
			return R.data(viewerService.getPrintData(req));
		} catch (Exception e) {
			return R.fail(e.getMessage());
		}
	}

	/**
	 * 票证管理 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入viewer")
	public R submit(@Valid @RequestBody ViewerEntity viewerEntity) {
		List<ViewerEntity> list = new ArrayList<>();
		list.add(viewerEntity);
		return R.status(viewerService.saveOrUpdate(viewerEntity));
	}

	/**
	 * 票证管理 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(viewerService.deleteLogic(Func.toLongList(ids)));
	}
	/**
	 * 导入票证
	 */
	@PostMapping("import-viewer")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导入用户", notes = "传入excel")
	public R importUser(MultipartFile file, HttpServletResponse response) {
		ViewerImporter viewerImporter = new ViewerImporter(viewerService);
		ExcelUtil.save(file, viewerImporter, ViewerExcel.class);

/*		List<ViewerExcel> list = new ArrayList<>();
		ExcelUtil.export(response, "票证数据" + DateUtil.time(), "票证错误数据表", list, ViewerExcel.class);*/
		return R.success("1");
	}

	/**
	 * 入场权限查询
	 */
//	@PostMapping("/access-query")
//	@ApiOperationSupport(order = 12)
//	@ApiOperation(value = "入场权限查询", notes = "传入viewer")
//	public  R<List<TerAccessViewerVO>> accessQuery(@Valid @RequestBody ViewerEntity viewer) {
//		if(StringUtil.isBlank(viewer.getName())  && StringUtil.isBlank(viewer.getIdCard()) && StringUtil.isBlank(viewer.getSwipeCard()) && StringUtil.isBlank(viewer.getName()) && StringUtil.isBlank(viewer.getTwoCode())){
//			return  R.data(null);
//		}
//		ViewerDTO viewerDTO = BeanUtil.copy(viewer, ViewerDTO.class);
//		return R.data(iaccessQueryService.accessQuery(viewerDTO));
//	}
	/**
	 * 链路撤销
	 */
//	@PostMapping("/revoke")
//	@ApiOperationSupport(order = 12)
//	@ApiOperation(value = "链路撤销", notes = "传入 importLink ID")
//	public  R revoke(@Valid @RequestBody ImportLinkEntity importLink) {
//
//		return  R.status(true);
//	}

	/**
	 * 导出 票证
	 */
	@GetMapping("export-viewer")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导出 票证", notes = "导出 票证")
	public void exportViewer(ViewerEntity viewerEntity,HttpServletResponse response) {
		List<ViewerExcel> list = viewerService.exportQuery(viewerEntity);
		ExcelUtil.export(response, "票证数据" + DateUtil.time(), "票证数据", list, ViewerExcel.class);
	}

	/**
	 * 删除 人脸
	 */
	@GetMapping("face-delete")
	@ApiOperationSupport(order = 12)
	@ApiOperation(value = "导出 票证", notes = "导出 票证")
	public R aa() {
		List<ViewerEntity> add = viewerService.add();
		add.forEach(o->{
			AipFaceClient.deleteFace(o.getId().toString(),"1640611560205410306");
			TxCommon.deletePersonFromGroup(o.getId().toString(),"1640611560205410306");
		});
		return R.data("111");
	}

}
